<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="挂号单号" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入挂号单号" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="科室" prop="deptId">
        <el-input v-model="queryParams.deptId" placeholder="请输入科室" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
      <el-form-item label="接诊类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择接诊类型" clearable>
          <el-option v-for="dict in doctor_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="医生id" prop="doctorId">
        <el-input v-model="queryParams.doctorId" placeholder="请输入医生id" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="患者id" prop="patientId">
        <el-input v-model="queryParams.patientId" placeholder="请输入患者id" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
      <el-form-item label="就诊状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择就诊状态" clearable>
          <el-option v-for="dict in fy_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['jy:registrationcost:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['jy:registrationcost:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['jy:registrationcost:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['jy:registrationcost:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="registrationcostList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="挂号单号" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="patientInfo.name" />
      <el-table-column label="性别" align="center" prop="patientInfo.sex">
        <template #default="scope">
          <span v-if="scope.row.patientInfo && scope.row.patientInfo.sex != null">
            <dict-tag :options="sys_user_sex" :value="scope.row.patientInfo.sex" />
          </span>
        </template>
      </el-table-column>
      <el-table-column label="就诊状态" align="center" prop="state">
        <template #default="scope">
          <dict-tag :options="fy_status" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="接诊医生" align="center" prop="doctorInfo.nickName" />
      <el-table-column label="科室" align="center" prop="sysDept.deptName" />
      <el-table-column label="接诊类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="doctor_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="挂号费" align="center" prop="doctorRegistationcost.registrationCost">
        <template #default="scope">
          <span>{{ scope.row.doctorRegistationcost.registrationCost }} 元</span>
        </template>
      </el-table-column>
      <el-table-column label="挂号类型" align="center" prop="doctorRegistationcost.registrationName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['jy:registrationcost:edit']">查看详情</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['jy:registrationcost:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改挂号记录对话框 -->
    <!-- <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="registrationcostRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="科室" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入科室" />
        </el-form-item>
        <el-form-item label="接诊类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择接诊类型">
            <el-option v-for="dict in doctor_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医生id" prop="doctorId">
          <el-input v-model="form.doctorId" placeholder="请输入医生id" />
        </el-form-item>
        <el-form-item label="挂号费" prop="money">
          <el-input v-model="form.money" placeholder="请输入挂号费" />
        </el-form-item>
        <el-form-item label="患者id" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入患者id" />
        </el-form-item>
        <el-form-item label="就诊状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择就诊状态">
            <el-option v-for="dict in fy_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog> -->
    <!-- 查看详情信息弹出框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body style="font-weight: 600;">
      <div class="dialog-content">
        <div class="section-header">患者信息</div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;编号">
              <el-tag>{{ form.patientId }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;姓名">
              <el-tag>{{ form.patientName }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;性别">
              <el-tag><dict-tag :options="sys_user_sex" :value="form.patientSex" /></el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;年龄">
              <el-tag>{{ form.patientAge }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;生日">
              <el-tag>{{ form.patientBirthday }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;手机号">
              <el-tag>{{ form.patientPhone }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="身份证号">
              <el-tag>{{ form.patientIdNumber }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="会员等级">
              <dict-tag :options="doctor_vip" :value="form.patientVipType" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;民族">
              <el-tag>{{ form.patientNation }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="婚姻状况">
              <el-tag><dict-tag :options="doctor_marriage" :value="form.patientMarriagestatus" /></el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="家庭住址">
              <el-tag>{{ form.patientAddress }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学历信息">
              <el-tag><dict-tag :options="doctor_education" :value="form.patientEducation" /></el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="职业类型">
              <el-tag><dict-tag :options="doctor_career" :value="form.patientCareer" /></el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工作单位">
              <el-tag>{{ form.patientWorkUnit }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <hr>
        <div class="section-header">挂号信息</div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;单号">
              <el-tag>{{ form.registationId }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="挂号类别">
              <el-tag>{{ form.registationType }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="挂号费用">
              <el-tag>{{ form.registrationCost }} 元</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="挂号时间">
              <el-tag>{{ form.registrationTime }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <hr>
        <div class="section-header">医生信息</div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;编号">
              <el-tag>{{ form.doctorId }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;姓名">
              <el-tag>{{ form.doctorName }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;科室">
              <el-tag>{{ form.doctorDept }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;性别">
              <el-tag><dict-tag :options="sys_user_sex" :value="form.doctorSex" /></el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;邮箱">
              <el-tag>{{ form.doctorEmail }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;手机号">
              <el-tag>{{ form.doctorPhonenumber }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="在职状态">
              <dict-tag :options="doctor_doctor_status" :value="form.doctorStatus" />
            </el-form-item>
          </el-col>
        </el-row>
        <hr>
        <div class="section-header">科室信息</div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;编号">
              <el-tag>{{ form.deptId }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名称">
              <el-tag>{{ form.deptName }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话">
              <el-tag>{{ form.deptPhone }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;邮箱">
              <el-tag>{{ form.deptEmail }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科室状态">
              <dict-tag :options="doctor_dept_status" :value="form.deptStatus" />
            </el-form-item>
          </el-col>
        </el-row>

      </div>
      <template #footer>
        <div class="dialog-footer">
          <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Registrationcost">
import { listRegistrationcost, getRegistrationcost, delRegistrationcost, addRegistrationcost, updateRegistrationcost } from "@/api/jy/registrationcost";
import { listUser } from "@/api/system/user";
import { listPatient } from "@/api/chy/patient";
import { onMounted, ref } from 'vue';

// 定义数据加载状态
const isDataLoaded = ref(false);

const { proxy } = getCurrentInstance();
const { doctor_type, fy_status, sys_user_sex } = proxy.useDict('doctor_type', 'fy_status', 'sys_user_sex');
const { doctor_vip, doctor_marriage, doctor_education, doctor_career, doctor_doctor_status, doctor_dept_status, doctor_registration_status } = proxy.useDict('sys_user_sex', 'doctor_vip', 'doctor_marriage', 'doctor_education', 'doctor_career', 'doctor_doctor_status', 'doctor_dept_status', 'doctor_registration_status');


const registrationcostList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    deptId: null,
    type: null,
    doctorId: null,
    createTime: null,
    patientId: null,
    state: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

//定义doctorInfo，用于存储医生信息
const doctorInfo = ref([]);

//定义patientInfo，用于存储患者信息
const patientInfo = ref([]);

function getPatientInfo() {
  return listPatient().then(response => {
    patientInfo.value = response.rows;
  });
}

function getDoctorInfo() {
  return listUser().then(response => {
    doctorInfo.value = response.rows;
  });
}

/** 查询挂号记录列表 */
function getList() {
  loading.value = true;
  listRegistrationcost(queryParams.value).then(response => {
    const orders = response.rows;
    orders.forEach(order => {
      const patient = patientInfo.value.find(p => p.id === order.patientId);
      order.patientInfo = patient || {};
      const doctor = doctorInfo.value.find(d => d.userId === order.doctorId);
      order.doctorInfo = doctor || {};
    });
    registrationcostList.value = orders;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deptId: null,
    type: null,
    doctorId: null,
    money: null,
    createTime: null,
    createPeople: null,
    patientId: null,
    state: null
  };
  proxy.resetForm("registrationcostRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加挂号记录";
}

/** 修改按钮操作 */
// function handleUpdate(row) {
//   reset();
//   const _id = row.id || ids.value
//   getRegistrationcost(_id).then(response => {
//     form.value = response.data;
//     open.value = true;
//     title.value = "修改挂号记录";
//   });
// }

// 查看详情按钮操作
function handleUpdate(row) {
  reset();
  getRegistrationcost(row.id).then(response => {
    form.value = {
      patientId: row.patientInfo.id,
      patientName: row.patientInfo.name,
      patientSex: row.patientInfo.sex,
      patientAge: row.patientInfo.age,
      patientBirthday: row.patientInfo.birthday,
      patientPhone: row.patientInfo.phone,
      patientIdNumber: row.patientInfo.idNumber,
      patientVipType: row.patientInfo.vipType,
      patientNation: row.patientInfo.nation,
      patientMarriagestatus: row.patientInfo.marriagestatus,
      patientAddress: row.patientInfo.address,
      patientEducation: row.patientInfo.education,
      patientCareer: row.patientInfo.career,
      patientWorkUnit: row.patientInfo.workUnit,
      doctorId: row.doctorInfo.userId,
      doctorName: row.doctorInfo.nickName,
      doctorDept: row.doctorInfo.dept.deptName,
      doctorEmail: row.doctorInfo.email,
      doctorPhonenumber: row.doctorInfo.phonenumber,
      doctorSex: row.doctorInfo.sex,
      doctorStatus: row.doctorInfo.status,
      deptId: row.sysDept.deptId,
      deptName: row.sysDept.deptName,
      deptPhone: row.sysDept.phone,
      deptEmail: row.sysDept.email,
      deptStatus: row.sysDept.status,
      registationId: row.doctorRegistationcost.id,
      registationType: row.doctorRegistationcost.registrationName,
      registrationCost: row.doctorRegistationcost.registrationCost,
      registrationTime: row.doctorRegistationcost.time
    };
    open.value = true;
    title.value = "修改挂号记录";
  });
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["registrationcostRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRegistrationcost(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRegistrationcost(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除挂号记录编号为"' + _ids + '"的数据项？').then(function () {
    return delRegistrationcost(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('jy/registrationcost/export', {
    ...queryParams.value
  }, `registrationcost_${new Date().getTime()}.xlsx`)
}


onMounted(() => {
  Promise.all([getPatientInfo(), getDoctorInfo()]).then(() => {
    isDataLoaded.value = true;
    // 可选：在数据加载完成后立即调用 getList
    getList();
  }).catch(error => {
    console.error('加载数据失败:', error);
  });
});
</script>

<style scoped>
.dialog-content {
  padding: 20px;
}

.section-header {
  font-weight: bold;
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
  margin-top: 30px;
}
.section-header:first-child {
  margin-top: 0; 
}

.el-row {
  margin-bottom: 1px;
}

.el-col {
  padding: 1px 0;
}

.el-tag {
  background-color: #dfe8ff;
  border-radius: 4px;
  padding: 15px 12px;
  font-size: 14px;
  color: #333;
}
</style>